<template>
  <div class="system-home">
    <img src="@/assets/images/1.png" class="system-home-title" />
    <div class="system-home__menu">
      <div class="menu__item" v-for="item in homeNavMenu" :key="item.id" :style="{
        backgroundColor: item.color,
        width: `calc(${width} / ${Math.min(4, homeNavMenu.length)})`,
        height: `calc(${width} / ${Math.min(4, homeNavMenu.length)})`
      }" @click="navMenuGoPath(item)">
        <img :src="setSrc(item.icon)" alt="" />
        {{ item.title }}
      </div>
    </div>
    <div class="notify" v-if="info">
      <img src="@/assets/images/2.png" alt="" class="notify-1" />
      <!--<div class="notify-tx" @click="toPage(it)">-->
      <!--  {{ info.title || '' }}-->
      <!--</div>-->
      <el-carousel class="carousel" direction="vertical" pause-on-hover loop :autoplay="true">
        <el-carousel-item v-for="item in info" :key="item">
          <div class="notify-tx" @click="MRichPopupOpen(it.businessKey, it.title)" v-for="it in item">
            {{ it.title || '' }}
          </div>
        </el-carousel-item>
      </el-carousel>
      <div class="notify-btn" @click="toDetail">查看更多</div>
    </div>
    <div class="system-home__navbar">
      <div class="navbar__item" v-for="item in barNavMenu" :key="item.id">
        <span @click="navMenuGoPath(item)">
          <img :src="setSrc(item.icon)" alt="" />
          {{ item.title }}
        </span>
      </div>
    </div>
    <MRichPopup ref="MRichPopupRef" />
  </div>
</template>

<script setup lang="ts">
import MRichPopup from '@/views/notify/popup/popup.vue'
import { useRouter } from 'vue-router'
import { homeNavMenu, barNavMenu } from '@/config/navigationMenu'
import { useUserStore } from '@/store'
import { computed, onMounted, ref } from 'vue'
import { noticePage } from '@/api/spotCheck/spotCheck'
import db from '@/storage'
const useStore = useUserStore()

const MRichPopupRef = ref()
const MRichPopupOpen = (id, title) => {
  MRichPopupRef.value.show(id, title)
}

defineOptions({
  name: 'SystemHomeMenu'
})
const width = '64vw'

const info = ref('')
onMounted(() => {
  noticePage({ pageNum: 1, pageSize: 9 }).then((res) => {
    // 将数据分割成每组三条
    info.value = chunkArray(res.data.list, 3)
    console.log('info', info.value)
  })
})
// 将一维数组分割成二维数组
function chunkArray(array, size) {
  const result = []
  for (let i = 0; i < array.length; i += size) {
    result.push(array.slice(i, i + size))
  }
  return result
}
const toPage = (it) => {
  switch (it.source.value) {
    case 1:
      router.push({
        path: '/spotCheck/informationDisclosure',
        query: {
          id: it.businessKey
        }
      })
  }
}

const router = useRouter()

// 引入图标
const setSrc = (name?: string) => {
  return new URL(`../../../assets/imgs/home/${name}.png`, import.meta.url).href
}

const getNavigationMenuMap = computed(() => {
  return useStore.navigationMenuMap
})
//跳转
const navMenuGoPath = (item) => {
  let menu = useStore.navigationMenuMap.get(item.path)
  console.log('menu', menu)

  if (menu != undefined) {
    if (Array.isArray(menu.children) && menu.children.length > 0) {
      db.set<string>('currentMenuName', menu.children[0].label)
      router.push({
        path: menu.children[0].path
      })
    }
  }
}

const toDetail = () => {
  router.push({
    name: 'Notify'
  })
}
</script>

<style lang="scss" scoped>
.system-home {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: url('@/assets/imgs/home/backgroundImg.png');
  background-size: 100% 100%;

  .system-home-title {
    width: 822px;
    height: 140px;
    margin-bottom: 50px;
  }

  &__menu {
    display: flex;
    flex-wrap: wrap;
    //width: v-bind(width);
  }

  .menu__item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    max-width: 360px;
    max-height: 360px;
    margin-right: 27px;
    font-weight: 500;
    font-size: 28px;
    color: #333333;
    text-align: center;
    cursor: pointer;
    background: url('@/assets/imgs/home/bg.png');
    background-size: 100% 100%;

    img {
      width: 6vw;
      height: 6vw;
      margin-bottom: 39px;
    }

    box-sizing: border-box;
    border: 1px solid #ffffff;
  }

  .menu__item:last-child {
    margin-right: 0;
  }

  // navbar
  &__navbar {
    position: fixed;
    bottom: 50px;
    left: 0;
    display: flex;
    width: 100%;
  }

  .navbar__item {
    flex: 1;
    font-weight: 400;
    font-size: 28px;
    color: #666666;
    display: flex;
    align-items: center;
    justify-content: center;

    span {
      display: flex;
      align-items: center;
      cursor: pointer;
    }

    img {
      width: 38px;
      height: 38px;
      margin-right: 15px;
    }
  }
}

.notify {
  width: calc(64vw + 81px);
  height: 138px;
  background: rgba(255, 255, 255, 0.6);
  border-radius: 8px 8px 8px 8px;
  display: flex;
  align-items: center;
  box-sizing: border-box;
  padding: 19px 35px;
  margin-top: 20px;

  //position: fixed;
  //top: 90px;
  //left: 0;
  //right: 0;
  //margin: auto;
  .notify-1 {
    width: 70px;
    height: 91px;
  }

  .notify-2 {
    width: 70px;
    height: 14px;
    margin: 0 21px 0 11px;
  }

  .carousel {
    flex: 1;
    min-width: 0;
    padding: 0 21px;
    margin: 0 30px;
    height: 100%;
    border-left: 1px solid #b9cfed;
    border-right: 1px solid #b9cfed;
  }

  .notify-tx {
    width: 100%;
    height: 26px;
    font-family: 思源黑体 CN, 思源黑体 CN;
    font-weight: 400;
    font-size: 18px;
    color: #333333;
    line-height: 0px;
    text-align: left;
    font-style: normal;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    line-height: 26px;
    margin-bottom: 10px;
    cursor: pointer;
  }
  .notify-tx:hover {
    color: #0c7cff;
  }
  .notify-btn {
    //width: 56px;
    font-family: 思源黑体 CN, 思源黑体 CN;
    font-weight: 400;
    font-size: 18px;
    color: #1C85FF;
    line-height: 0px;
    text-align: right;
    font-style: normal;
    cursor: pointer;
  }
}

:deep(.el-carousel__indicators) {
  display: none !important;
}

.menu__item {
  position: relative;
}
</style>
